/***************************************************************************
 创建者: hualei
 开始时间: 2018-12-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  ROLLER 滚筒指令具体实现
 ***************************************************************************/
#include "programengine.h"
#include "programset.h"
#include "mccontrollerthread.h"
#include "../EcatCore/robotio.h"
#include "rollerinstructionlogic.h"
#include "GeneralDefine.h"
#include "messagelog.h"
#include "liftmechanisminstructionlogic.h"

#define D_WAIT_SLEEP_TIME 10000.0
#define D_ROLLER_LOGIC_FILE_PATH "globalConfig/programLogic/rollerLogic.xml"

E_ROLLER_STATUS RollerInstructionLogic::getRollerMechanismStatus()
{
    if(false==isRuned)
    {
        judgeCurrentStatus();
    }
    return basicStatus;
}

int RollerInstructionLogic::setRollerMechanismConfig(RollerMechanismConfig configIn)
{
    mechanismConfig=configIn;
    return writeConfigFile(configFilePath,configIn);
}

int RollerInstructionLogic::getRollerMechanismConfig(RollerMechanismConfig &configOut)
{
    qDebug()<<"getRollerMechanismConfig";
    configOut=mechanismConfig;
    return 1;
}

RollerInstructionLogic::RollerInstructionLogic(RobotIo *ioControllerIn, int robotIdIn,
             ProgramSet *programSetIn, ProgramEngine *programEngineIn,
              McControllerThread *programMcControllerIn, LiftMechanismInstructionLogic *liftLogicIn)
{
    robotId = robotIdIn;
    programSet = programSetIn;
    programEngine = programEngineIn;
    mcController = programMcControllerIn;
    ioController = ioControllerIn;
    liftLogic=liftLogicIn;
    isInitialStatus=false;
    isRuned=false;
    basicStatus=E_ROLLER_STATUS_EMPTY;
//    rollerMotorType=1;

    configFilePath=D_TCRCONTROLLER_DATA_PATH;
    configFilePath+=D_ROLLER_LOGIC_FILE_PATH;
    int tmpReturn=readConfigFile(configFilePath);
    if(1!=tmpReturn)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine", 12070, robotId);
    }
    else
    {
        judgeCurrentStatus();
        isInitialStatus=true;
    }
}

int RollerInstructionLogic::stop()
{
    commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
    return 1;
}

int RollerInstructionLogic::clearAllMsg()
{
    judgeCurrentStatus();
}

E_PROGRAM_LOOP_RESULT RollerInstructionLogic::startRoller(int programNumIn, const RollerInstruction &instructionIn, int lineIndex)
{
    Q_UNUSED(lineIndex);

    E_PROGRAM_LOOP_RESULT tmpResult=roller_run(instructionIn);
    if(E_PROGRAM_LOOP_RESULT_NORMAL!=tmpResult && E_PROGRAM_LOOP_RESULT_JUMP!=tmpResult)
    {
        return tmpResult;
    }
    else if(E_PROGRAM_LOOP_RESULT_JUMP==tmpResult)
    {
         QString tmpjumpLabel=QString::number(instructionIn.jmpLbValue);
        return programEngine->startJump(programNumIn,tmpjumpLabel);
    }

    programEngine->calculateNextLineToRun(programNumIn);
    return programEngine->isTheLastInstruction(programNumIn);
}

E_PROGRAM_LOOP_RESULT RollerInstructionLogic::roller_run(const RollerInstruction &instructionIn)
{
    qDebug()<<"roller_run　,rollerDirectionType"<<instructionIn.rollerDirectionType<<"TIMEOUT"<<instructionIn.timeOutValue;

    isRuned=true;
    if(false==isInitialStatus)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12070, robotId);
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    if(instructionIn.instructionType != ROLLER)
    {
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }


    int tmpWaitTimeOut;
    if(instructionIn.timeOutValue>mechanismConfig.rollerTimeOut)
    {
        tmpWaitTimeOut=instructionIn.timeOutValue;
    }
    else
    {
        tmpWaitTimeOut=mechanismConfig.rollerTimeOut;
    }
    qDebug()<<"tmpWaitTimeOut"<<tmpWaitTimeOut;

    E_PROGRAM_LOOP_RESULT tmpBlockResult;
    if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_LOAD
                     || instructionIn.rollerDirectionType == E_ROLLER_RIGHT_LOAD)
    {

        if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_LOAD)
        {// 左侧上货
            judgeCurrentStatus();
            if(E_ROLLER_STATUS_LOADED==basicStatus)
            {
                addMsg(ENUM_MSG_REMIND, "ProgramEngine", "ProgramEngine",  12076, robotId);
                qDebug()<<"左侧上货，已经处于载货状态，不运行";
                return E_PROGRAM_LOOP_RESULT_NORMAL;
            }
            qDebug()<<"左侧上货，左阻挡下降,右阻挡上升";
            if(1)
            {
                commonRollerRun(E_ROLLER_CONTROL_POSITIVE,mechanismConfig.rollerDirectionExchange);
            }
            basicStatus=E_ROLLER_STATUS_LIN;
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_DOWN,mechanismConfig.leftBlockType,mechanismConfig.leftBlockDo1_index,mechanismConfig.leftBlockDo2_index,
                                                mechanismConfig.leftTopSensor_index,mechanismConfig.leftButtomSensor_index,1000,
                                          mechanismConfig.leftBlockBType,mechanismConfig.leftBlockDo1B_index,mechanismConfig.leftBlockDo2B_index,
                                          mechanismConfig.leftTopSensorB_index,mechanismConfig.leftButtomSensorB_index,1000,
                                                  mechanismConfig.blockWaitTime);
            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12074, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }

            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_RISE,mechanismConfig.rightBlockType,mechanismConfig.rightBlockDo1_index,mechanismConfig.rightBlockDo2_index,
                                                mechanismConfig.rightTopSensor_index,mechanismConfig.rightButtomSensor_index,1000
                                            ,mechanismConfig.rightBlockBType,mechanismConfig.rightBlockDo1B_index,mechanismConfig.rightBlockDo2B_index,
                                              mechanismConfig.rightTopSensorB_index,mechanismConfig.rightButtomSensorB_index,1000
                                                  ,mechanismConfig.blockWaitTime);
            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12075, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_POSITIVE,mechanismConfig.rollerDirectionExchange);
        }
        else if(instructionIn.rollerDirectionType == E_ROLLER_RIGHT_LOAD)
        {
            judgeCurrentStatus();
            if(E_ROLLER_STATUS_LOADED==basicStatus)
            {
                addMsg(ENUM_MSG_REMIND, "ProgramEngine", "ProgramEngine",  12076, robotId);
                qDebug()<<"左侧上货，已经处于载货状态，不运行";
                return E_PROGRAM_LOOP_RESULT_NORMAL;
            }
            qDebug()<<"右侧上货，右阻挡下降,左阻挡上升";
            if(1)
            {
                commonRollerRun(E_ROLLER_CONTROL_NEGATIVE,mechanismConfig.rollerDirectionExchange);
            }
            basicStatus=E_ROLLER_STATUS_RIN;
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_DOWN,mechanismConfig.rightBlockType,mechanismConfig.rightBlockDo1_index,
                                                mechanismConfig.rightBlockDo2_index,mechanismConfig.rightTopSensor_index,mechanismConfig.rightButtomSensor_index,1000
                                                           ,mechanismConfig.rightBlockBType,mechanismConfig.rightBlockDo1B_index,
                                                           mechanismConfig.rightBlockDo2B_index,mechanismConfig.rightTopSensorB_index,mechanismConfig.rightButtomSensorB_index,1000
                                                           ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12075, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_RISE,mechanismConfig.leftBlockType,mechanismConfig.leftBlockDo1_index,
                                                mechanismConfig.rightBlockDo2_index,mechanismConfig.leftTopSensor_index,mechanismConfig.leftButtomSensor_index,1000
                                                             ,mechanismConfig.leftBlockBType,mechanismConfig.leftBlockDo1B_index,
                                                             mechanismConfig.rightBlockDo2B_index,mechanismConfig.leftTopSensorB_index,mechanismConfig.leftButtomSensorB_index,1000
                                                             ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12074, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_NEGATIVE,mechanismConfig.rollerDirectionExchange);
        }


        int sleepCount=0;
        while(false==isInCargoFinished(instructionIn.rollerDirectionType))
        {
            realTimeSleep(D_WAIT_SLEEP_TIME);
            sleepCount++;
            double totalWaitTime=sleepCount*D_WAIT_SLEEP_TIME;
            if(totalWaitTime>tmpWaitTimeOut*1000000)
            {
                // 操作超时
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                qDebug()<<"roller time out error";
                if(0==mechanismConfig.rollerMechanicalType)
                {
                    addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12071, robotId
                           , programEngine->getRunningLineFloat());
                }
                else
                {
                     basicStatus=E_ROLLER_STATUS_LOADED;
                     return E_PROGRAM_LOOP_RESULT_JUMP;
                }
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }

            //cease stop
            MC_TRAJECTORY_STATUS tmpStatus=(MC_TRAJECTORY_STATUS)mcController->mcControllerInterpreter->getTrajServerStatus();
            if(ENUM_TRAJECTORY_STOPPED == tmpStatus)
            {
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
               return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            else if(ENUM_TRAJECTORY_TOCEASE == tmpStatus || ENUM_TRAJECTORY_CEASING == tmpStatus
                     || ENUM_TRAJECTORY_CEASED == tmpStatus)
            {
                sleepCount=0;
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
            }
            else if(ENUM_TRAJECTORY_RUN == tmpStatus)
            {
                if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_LOAD)
                {
                    commonRollerRun(E_ROLLER_CONTROL_POSITIVE,mechanismConfig.rollerDirectionExchange);
                }
                else if(instructionIn.rollerDirectionType == E_ROLLER_RIGHT_LOAD)
                {
                    commonRollerRun(E_ROLLER_CONTROL_NEGATIVE,mechanismConfig.rollerDirectionExchange);
                }
            }

        }

        sleep(mechanismConfig.arriveDelayTime);

        //阻挡升起
        if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_LOAD)
        {
            qDebug()<<"左阻挡上升";
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_RISE,mechanismConfig.leftBlockType,mechanismConfig.leftBlockDo1_index,
                                                mechanismConfig.leftBlockDo2_index,mechanismConfig.leftTopSensor_index,mechanismConfig.leftButtomSensor_index,1000
                                                ,mechanismConfig.leftBlockBType,mechanismConfig.leftBlockDo1B_index,
                                                             mechanismConfig.leftBlockDo2B_index,mechanismConfig.leftTopSensorB_index,mechanismConfig.leftButtomSensorB_index,1000
                                                             ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12074, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);

        }
        else if(instructionIn.rollerDirectionType == E_ROLLER_RIGHT_LOAD)
        {
            qDebug()<<"右阻挡上升";
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_RISE,mechanismConfig.rightBlockType,mechanismConfig.rightBlockDo1_index,
                     mechanismConfig.rightBlockDo2_index, mechanismConfig.rightTopSensor_index,mechanismConfig.rightButtomSensor_index,1000
                      ,mechanismConfig.rightBlockBType,mechanismConfig.rightBlockDo1B_index,
                                       mechanismConfig.rightBlockDo2B_index, mechanismConfig.rightTopSensorB_index,mechanismConfig.rightButtomSensorB_index,1000
                                       ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12075, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
        }
         basicStatus=E_ROLLER_STATUS_LOADED;

    }

    else if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_DOWNLOAD ||
                        instructionIn.rollerDirectionType == E_ROLLER_RIGHT_DOWNLOAD)
    {
        // 左侧卸货
        if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_DOWNLOAD)
        {
            qDebug()<<"左侧卸货，左阻挡下降";
            basicStatus=E_ROLLER_STATUS_LOUT;
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_DOWN,mechanismConfig.leftBlockType,mechanismConfig.leftBlockDo1_index,
                           mechanismConfig.leftBlockDo2_index, mechanismConfig.leftTopSensor_index,mechanismConfig.leftButtomSensor_index,1000
                                       ,mechanismConfig.leftBlockBType,mechanismConfig.leftBlockDo1B_index,
                                       mechanismConfig.leftBlockDo2B_index, mechanismConfig.leftTopSensorB_index,mechanismConfig.leftButtomSensorB_index,1000
                                       ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12074, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_NEGATIVE,mechanismConfig.rollerDirectionExchange);
        }
        else if(instructionIn.rollerDirectionType == E_ROLLER_RIGHT_DOWNLOAD)
        {//右侧卸货
            qDebug()<<"右侧卸货，右阻挡下降";
            basicStatus=E_ROLLER_STATUS_ROUT;
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_DOWN,mechanismConfig.rightBlockType,mechanismConfig.rightBlockDo1_index,
                                       mechanismConfig.rightBlockDo2_index, mechanismConfig.rightTopSensor_index,mechanismConfig.rightButtomSensor_index,1000
                                       ,mechanismConfig.rightBlockBType,mechanismConfig.rightBlockDo1B_index,
                                       mechanismConfig.rightBlockDo2B_index, mechanismConfig.rightTopSensorB_index,mechanismConfig.rightButtomSensorB_index,1000
                                       ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12075, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {

                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_POSITIVE,mechanismConfig.rollerDirectionExchange);
        }


        int sleepCount=0;
        while(false==isInCargoFinished(instructionIn.rollerDirectionType))
        {
            realTimeSleep(D_WAIT_SLEEP_TIME);
            sleepCount++;
            double totalWaitTime=sleepCount*D_WAIT_SLEEP_TIME;
            if(totalWaitTime>tmpWaitTimeOut*1000000)
            {

                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);

                qDebug()<<"roller reverse time out error";
                if(0==mechanismConfig.rollerMechanicalType)
                {
                    addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12072, robotId
                           , programEngine->getRunningLineFloat());
                }
                else
                {
                    basicStatus=E_ROLLER_STATUS_EMPTY;
                    return E_PROGRAM_LOOP_RESULT_JUMP;
                }
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }

            //cease stop
            MC_TRAJECTORY_STATUS tmpStatus=(MC_TRAJECTORY_STATUS)mcController->mcControllerInterpreter->getTrajServerStatus();
            if(ENUM_TRAJECTORY_STOPPED == tmpStatus)
            {
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
               return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            else if(ENUM_TRAJECTORY_TOCEASE == tmpStatus || ENUM_TRAJECTORY_CEASING == tmpStatus
                     || ENUM_TRAJECTORY_CEASED == tmpStatus)
            {
                sleepCount=0;
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
            }
            else if(ENUM_TRAJECTORY_RUN == tmpStatus)
            {
                if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_DOWNLOAD)
                {
                    commonRollerRun(E_ROLLER_CONTROL_NEGATIVE,mechanismConfig.rollerDirectionExchange);
                }
                else if(instructionIn.rollerDirectionType == E_ROLLER_RIGHT_DOWNLOAD)
                {
                    commonRollerRun(E_ROLLER_CONTROL_POSITIVE,mechanismConfig.rollerDirectionExchange);
                }
            }


        }

        sleep(mechanismConfig.leaveDelayTime);
        //阻挡升起
        if(instructionIn.rollerDirectionType == E_ROLLER_LEFT_DOWNLOAD)
        {
            qDebug()<<"左阻挡上升";
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_RISE,mechanismConfig.leftBlockType,mechanismConfig.leftBlockDo1_index,
                                       mechanismConfig.leftBlockDo2_index, mechanismConfig.leftTopSensor_index,mechanismConfig.leftButtomSensor_index,1000
                                       ,mechanismConfig.leftBlockBType,mechanismConfig.leftBlockDo1B_index,
                                       mechanismConfig.leftBlockDo2B_index, mechanismConfig.leftTopSensorB_index,mechanismConfig.leftButtomSensorB_index,1000
                                       ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12074, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
        }
        else if(instructionIn.rollerDirectionType == E_ROLLER_RIGHT_DOWNLOAD)
        {
            qDebug()<<"右阻挡上升";
            tmpBlockResult=liftLogic->blockMechanismUpDown(E_LIFT_RISE,mechanismConfig.rightBlockType,mechanismConfig.rightBlockDo1_index,
                                       mechanismConfig.rightBlockDo2_index, mechanismConfig.rightTopSensor_index,mechanismConfig.rightButtomSensor_index,1000
                                       ,mechanismConfig.rightBlockBType,mechanismConfig.rightBlockDo1B_index,
                                       mechanismConfig.rightBlockDo2B_index, mechanismConfig.rightTopSensorB_index,mechanismConfig.rightButtomSensorB_index,1000
                                       ,mechanismConfig.blockWaitTime);

            if(E_PROGRAM_LOOP_RESULT_ERROR==tmpBlockResult)
            {
                addMsg(ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12075, robotId);
                commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
                basicStatus=E_ROLLER_STATUS_ERROR;
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
            else if(E_PROGRAM_LOOP_RESULT_CEASE_STOP==tmpBlockResult)
            {
                return E_PROGRAM_LOOP_RESULT_CEASE_STOP;
            }
            commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);
        }
        sleep(2);
        basicStatus=E_ROLLER_STATUS_EMPTY;
    }

    //完成后停止
    commonRollerRun(E_ROLLER_CONTROL_STOP,mechanismConfig.rollerDirectionExchange);

    return E_PROGRAM_LOOP_RESULT_NORMAL;
}

E_PROGRAM_LOOP_RESULT RollerInstructionLogic::roller_run(const RollerParameter &paraIn)
{
    RollerInstruction tmp_instruction;
    tmp_instruction.instructionType=ROLLER;
//    tmp_instruction.rollerType=paraIn.rollerType;
    tmp_instruction.rollerDirectionType=paraIn.rollerDirectionType;
    tmp_instruction.timeOutValue=paraIn.timeOutValue;
    return roller_run(tmp_instruction);
}

void RollerInstructionLogic::judgeCurrentStatus()
{
    if(NULL==ioController)
    {
        return ;
    }
    if(ioController->getDi(mechanismConfig.commonRightArriveSensor_index)
            || ioController->getDi(mechanismConfig.commonRightArriveSensor_index))
    {
        basicStatus=E_ROLLER_STATUS_LOADED;
    }
    else
    {
        basicStatus=E_ROLLER_STATUS_EMPTY;
    }
}

int RollerInstructionLogic::writeConfigFile(QString fileName, RollerMechanismConfig configIn)
{
    DomParser domparser;
    bool ok = domparser.openXml(fileName, QIODevice::ReadWrite);
    if( !ok )
    {
        qDebug() << "RollerInstructionLogic::readConfigFile readConfigFile ==== 配置文件打开失败：" << fileName;
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerMechanicalType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读结点失败：" << fileName;
        return -2;
    }
    domparser.writeXml(domNode, configIn.rollerMechanicalType);


    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonLeftLeaveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonLeftSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.writeXml(domNode, configIn.commonLeftLeaveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonRightLeaveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonRightSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.writeXml(domNode, configIn.commonRightLeaveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonLeftArriveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonLeftSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.writeXml(domNode, configIn.commonLeftArriveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonRightArriveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonRightSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.writeXml(domNode, configIn.commonRightArriveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonMotorPositiveDo_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonMotorPositiveDo_index结点失败：" << fileName;
        return -4;
    }
    domparser.writeXml(domNode, configIn.commonMotorPositiveDo_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonMotorReverseDo_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonMotorReverseDo_index结点失败：" << fileName;
        return -4;
    }
    domparser.writeXml(domNode,configIn.commonMotorReverseDo_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "blockWaitTime", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.blockWaitTime);

    //block1
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftTopSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftTopSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftButtomSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftButtomSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightTopSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightTopSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightButtomSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightButtomSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo1_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftBlockDo1_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo2_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftBlockDo2_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo1_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightBlockDo1_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo2_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightBlockDo2_index);


    int tmpType;
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    tmpType=configIn.leftBlockType;
    domparser.writeXml(domNode, tmpType);


    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    tmpType=configIn.rightBlockType;
    domparser.writeXml(domNode, tmpType);

    //block2
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftTopSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftTopSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftButtomSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftButtomSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightTopSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightTopSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightButtomSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightButtomSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo1B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftBlockDo1B_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo2B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leftBlockDo2B_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo1B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightBlockDo1B_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo2B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rightBlockDo2B_index);


    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockBType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    tmpType=configIn.leftBlockBType;
    domparser.writeXml(domNode, tmpType);


    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockBType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    tmpType=configIn.rightBlockBType;
    domparser.writeXml(domNode, tmpType);

    //
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerMotorType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rollerMotorType);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerTimeOut", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rollerTimeOut);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leaveDelayTime", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.leaveDelayTime);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "arriveDelayTime", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.arriveDelayTime);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerDirectionExchange", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.writeXml(domNode, configIn.rollerDirectionExchange);

    domparser.closeXml(true);
    return 1;
}

int RollerInstructionLogic::readConfigFile(const QString &fileName)
{
    DomParser domparser;
    bool ok = domparser.openXml(fileName, QIODevice::ReadOnly);
    if( !ok )
    {
        qDebug() << "RollerInstructionLogic::readConfigFile readConfigFile ==== 配置文件打开失败：" << fileName;
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerMechanicalType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读结点失败：" << fileName;
        return -2;
    }
    domparser.readXml(domNode, mechanismConfig.rollerMechanicalType);


    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonLeftLeaveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonLeftSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.readXml(domNode, mechanismConfig.commonLeftLeaveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonRightLeaveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonRightSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.readXml(domNode, mechanismConfig.commonRightLeaveSensor_index);


    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonLeftArriveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonLeftSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.readXml(domNode, mechanismConfig.commonLeftArriveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonRightArriveSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonRightSensor_index结点失败：" << fileName;
        return -4;
    }
    domparser.readXml(domNode, mechanismConfig.commonRightArriveSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonMotorPositiveDo_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonMotorPositiveDo_index结点失败：" << fileName;
        return -4;
    }
    domparser.readXml(domNode, mechanismConfig.commonMotorPositiveDo_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "commonMotorReverseDo_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 读取commonMotorReverseDo_index结点失败：" << fileName;
        return -4;
    }
    domparser.readXml(domNode,mechanismConfig.commonMotorReverseDo_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "blockWaitTime", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.blockWaitTime);

    //block 1
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftTopSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftTopSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftButtomSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftButtomSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightTopSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightTopSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightButtomSensor_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightButtomSensor_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo1_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftBlockDo1_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo2_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftBlockDo2_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo1_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightBlockDo1_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo2_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightBlockDo2_index);



    int tmpType;
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, tmpType);
    mechanismConfig.leftBlockType=(E_BLOCK_TYPE)tmpType;

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, tmpType);
    mechanismConfig.rightBlockType=(E_BLOCK_TYPE)tmpType;

    //block 2
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftTopSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftTopSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftButtomSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftButtomSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightTopSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightTopSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightButtomSensorB_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightButtomSensorB_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo1B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftBlockDo1B_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockDo2B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leftBlockDo2B_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo1B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightBlockDo1B_index);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockDo2B_index", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rightBlockDo2B_index);



    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leftBlockBType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, tmpType);
    mechanismConfig.leftBlockBType=(E_BLOCK_TYPE)tmpType;

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rightBlockBType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, tmpType);
    mechanismConfig.rightBlockBType=(E_BLOCK_TYPE)tmpType;



    //
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerMotorType", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rollerMotorType);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerTimeOut", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rollerTimeOut);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "leaveDelayTime", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.leaveDelayTime);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "arriveDelayTime", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.arriveDelayTime);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "rollerDirectionExchange", ok );
    if( !ok )
    {
        qDebug() << "readConfigFile ==== 结点失败：" << fileName;
        return -5;
    }
    domparser.readXml(domNode, mechanismConfig.rollerDirectionExchange);

    domparser.closeXml();
    return 1;
}

bool RollerInstructionLogic::isInCargoFinished(EM_ROLLER_DIRECTION_TYPE cargoMethod)
{

        switch(cargoMethod)
        {
        case E_ROLLER_LEFT_LOAD:
        {
                return ioController->getDi(mechanismConfig.commonRightArriveSensor_index);
        }
        case E_ROLLER_RIGHT_LOAD:
        {
                return ioController->getDi(mechanismConfig.commonLeftArriveSensor_index);
        }
        case E_ROLLER_LEFT_DOWNLOAD:
        {
            if(false==ioController->getDi(mechanismConfig.commonLeftLeaveSensor_index)&&
                    false==ioController->getDi(mechanismConfig.commonRightArriveSensor_index))
            {
                return true;
            }
                return false;
        }
        case E_ROLLER_RIGHT_DOWNLOAD:
        {
            if(false==ioController->getDi(mechanismConfig.commonLeftArriveSensor_index)&&
                    false==ioController->getDi(mechanismConfig.commonRightLeaveSensor_index))
            {
                return true;
            }
                return false;
        }
        }

    return false;
}



void RollerInstructionLogic::realTimeSleep(long long umicoroSecond)
{
    // 处理qt事件
    QCoreApplication::processEvents();

    struct timespec time;
    clock_gettime(CLOCK_REALTIME,&time);
    time.tv_nsec += umicoroSecond * 1000;
    while(time.tv_nsec >= NSEC_PER_SEC)
    {
        time.tv_nsec -= NSEC_PER_SEC;
        ++time.tv_sec;
    }
    clock_nanosleep(CLOCK_REALTIME,TIMER_ABSTIME,&time,NULL);
}


int RollerInstructionLogic::commonRollerRun(E_ROLLER_CONTROL control, bool isExchangeDirectionIn)
{
//    qDebug()<<"commonRollerRun control"<<control;

    if(E_ROLLER_CONTROL_POSITIVE==control)
    {
        if(0==mechanismConfig.rollerMotorType)
        {
            if(isExchangeDirectionIn)
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, false);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, true);
            }
            else
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, true);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, false);
            }

        }
        else
        {
            if(isExchangeDirectionIn)
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, true);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, true);
            }
            else
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, false);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, true);
            }

        }

    }
    else if(E_ROLLER_CONTROL_NEGATIVE==control)
    {
        if(0==mechanismConfig.rollerMotorType)
        {
            if(isExchangeDirectionIn)
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, true);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, false);
            }
            else
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, false);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, true);
            }

        }
        else
        {
            if(isExchangeDirectionIn)
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, false);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, true);
            }
            else
            {
                ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, true);
                ioController->setDo(mechanismConfig.commonMotorReverseDo_index, true);
            }

        }

    }
    else
    {
        ioController->setDo(mechanismConfig.commonMotorPositiveDo_index, false);
        ioController->setDo(mechanismConfig.commonMotorReverseDo_index, false);
    }

    return 1;
}



bool RollerInstructionLogic::isInTop_block(int topArrivalIndex,int buttomArrivalIndex)
{
    if(NULL==ioController)
    {
        return false;
    }
    if(false==ioController->getDi(topArrivalIndex)
          || true==ioController->getDi(buttomArrivalIndex))
    {
        return false;
    }
    return true;
}

bool RollerInstructionLogic::isInButtom_block(int topArrivalIndex, int buttomArrivalIndex)
{
    if(NULL==ioController)
    {
        return false;
    }
    if(true==ioController->getDi(topArrivalIndex)
          || false==ioController->getDi(buttomArrivalIndex))
    {
        return false;
    }
    return true;
}

bool RollerInstructionLogic::isInMiddle_block(int middleArrivalIndex, int buttomArrivalIndex, int topArrivalIndex)
{
    if(NULL==ioController)
    {
        return false;
    }
    if(true==ioController->getDi(middleArrivalIndex)
          && false==ioController->getDi(buttomArrivalIndex)
            && false==ioController->getDi(topArrivalIndex))
    {
        return true;
    }
    return false;
}


void RollerInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Q_UNUSED(parameter2);
    Q_UNUSED(parameter3);
    Q_UNUSED(parameter4);

    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {
    case 12076:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("滚筒机构：已经处于载货状态，跳过载货命令!")+message;
        break;
    }
    case 12075:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("滚筒机构：右阻挡机构超时未到位!")+message;
        break;
    }
    case 12074:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("滚筒机构：左阻挡机构超时未到位!")+message;
        break;
    }
    case 3003:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行 跳转指令错误，没有相应的LABEL标签!")+message;
        break;
    }

    case 12070:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常:(roller滚筒配置文件初始化失败!)")+message;
        break;
    }
    case 12071:
    {

       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常:(滚筒载货运动超时!)")+message;

        break;
    }
    case 12072:
    {

       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常:(滚筒卸货运动超时!)")+message;

        break;
    }
    case 12073:
    {

       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常:(滚筒阻挡机构超时!)")+message;

        break;
    }
        default:
        {

        }
    }

    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
